<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Auth;
class Resume extends Model
{
    //
    protected $table = 'resume';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo('App\Models\Home\Users','uid','id');
    }

    public function assign_user()
    {
        return $this->belongsTo('App\Models\Home\Users','assign','id');
    }

    public function file()
    {
        return $this->belongsTo('App\Models\Home\File','attachment','id');
    }

    public function position()
    {
        return $this->belongsToMany('App\Models\Home\Position','deliver','resume_id','position_id');
    }

    public function position_class()
    {
        return $this->belongsTo('App\Models\Home\PositionClass','position','id');
    }

    public function deliver()
    {
        return $this->hasMany('App\Models\Home\Deliver', 'resume_id', 'id');
    }
    
    public function create_deliver_schedule($data)
    {
        return Db::table('deliver_schedule')->insert($data);
    }
    /*查询最近三天的未推荐简历记录*/
    public function get_resume_three(){

        $created_at = date('Y-m-d',time()-3600*24*3).' 00:00:00';

        $created_at2 = date('Y-m-d H:i:s',time());



        $uid = Auth::user()->id;

        $data = Db::select('SELECT a.*, b.name as position FROM u_resume as a  LEFT JOIN u_position_class as b ON a.position=b.id WHERE (a.uid = ? OR a.assign = ?) AND a.created_at > ? AND a.created_at < ?', [$uid,$uid,$created_at,$created_at2]);

        $data2 =[];
        foreach ($data as $k=>$user) {

            if(!Db::select('SELECT id FROM u_deliver WHERE resume_id = ?',[$user->id])){
                $data2[$k]['sex'] = config('base.sex')[$user->sex];
                $data2[$k]['id'] = $user->id;
                $data2[$k]['name'] = $user->name;
                $data2[$k]['position'] = $user->position;
                $data2[$k]['experience'] =  config('base.working_time')[$user->experience]['name'];
                $data2[$k]['education'] = config('base.education')[$user->education]['name'];
                $data2[$k]['state'] = config('base.working_state')[$user->state]['name'];
                $data2[$k]['created_at'] = date('Y-m-d ',strtotime($user->created_at));
                $data2[$k]['source'] = Db::table('users')->where('id',$user->uid)->value('username');
            }

        }

        return $data2;
    }
    public function download_log()
    {
        return $this->morphMany('App\Models\Home\DownloadLog', 'DownloadLog','type','cid');
    }

    public function SetAssign($obj){
        $obj->load(['deliver'=>function($query){
            $query->with(['check_schedule'=>function($query){
                $query->orderBy('id','desc');
            }]);
        }]);

        $flag = false;
        if (count($obj->deliver)){
            foreach ($obj->deliver as $item){
                if ($item->check_schedule && $item->check_schedule->result == 13){
                    $flag = true;
                    break;
                }
            }
        }

        if ($flag){
            return $obj;
        }

        $assign = $obj->assign;
        $assign_time = $obj->assign_time;
        $share = $obj->share;

        if($obj->assign){
            if($obj->assign_time <= time()){
                $obj->assign = '';
                $obj->assign_time = '';
                $obj->share = 1;
                //插一条跟进记录
            }else{
                $obj->share = 0;
            }
        }else{
            $obj->share = 1;
            $obj->assign = '';
            $obj->assign_time = '';
        }

        if($assign != $obj->assign||$assign_time != $obj->assign_time||$share != $obj->share){

            $obj->save();

        }

        return $obj;
    }


    public function getList($skip,$limit,$opt=array(),$in=array(),$user=true,$assign=true,$file=true,$all=false,$doesnt=null){
        
        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $before = self::get();

        foreach ($before as $k => $v){

            $this->SetAssign($v);

        }

        $query = Resume::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        if (!$all){
            $query = $query->skip($skip)->limit($limit);
        }

        if ($user){
            $query = $query->with(['user']);
        }

        if ($assign){
            $query = $query->with(['assign_user.frame.belong_user']);
        }

        if ($file){
            $query = $query->with(['file']);
        }

        if ($doesnt){
            $query->whereDoesntHave('position', function ($query) use($doesnt) {
                $query->where('position.id', $doesnt)->where('deliver.status',1);
            });
        }

        $list = $query->with(['position_class'])->get();

        foreach ($list as $k => $v){

            if ($user){
                $list[$k]->username = $v->user?$v->user->username:'';
                $user_type = $v->user?$v->user->type:0;
                switch ($user_type){
                    case 1:
                        $list[$k]->user_type = '内部推荐 - ';
                        break;
                    case 2:
                        $list[$k]->user_type = '兼职推荐 - ';
                        break;
                    default:
                        $list[$k]->user_type = '自投';
                }
            }

            if ($assign){
                $list[$k]->assign_username = $v->assign_user?$v->assign_user->username:'';
                $list[$k]->team_username = $v->assign_user?$v->assign_user->frame?$v->assign_user->frame->belong_user?$v->assign_user->frame->belong_user->username:'':'':'';
            }

            if ($file){
                $list[$k]->file_name = $v->file?$v->file->name:'';
            }
            $list[$k]->birthday = date('Y-m',strtotime($v->birthday));
            switch ($v->sex){
                case 1:
                    $list[$k]->sex_name = '男';
                    break;
                case 2:
                    $list[$k]->sex_name = '女';
                    break;
                default:
                    $list[$k]->sex_name = '未知';

            }
            $list[$k]->nationality_name = $v->nationality?config('base.country.'.$v->nationality.'.name'):'';
            $list[$k]->education_name = $v->education?config('base.education.'.$v->education.'.name'):'';
            $list[$k]->experience_name = $v->experience?config('base.working_time.'.$v->experience.'.name'):'';
            $list[$k]->position_name = $v->position_class?$v->position_class->name:'';
            $list[$k]->state_name = $v->status?config('base.working_state.'.$v->status.'.name'):'';
        }

        return $list;
    }

    public function getCount($opt=array(),$in=array()){

        $query = Resume::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        return $query->count();
    }


}
